﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;
using System.Data.SqlClient;
using Common;
using BUS;
using DAO;
public partial class process : System.Web.UI.Page
{
    private Encrypt _objEncrypt = new Encrypt();
    BUS_WRUSER _busUser = new BUS_WRUSER();
    BUS_WRCATEGORY _buswrcategory = new BUS_WRCATEGORY();
    BUS_WRCATEGORYLV1 _buswrcategorylv1 = new BUS_WRCATEGORYLV1();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!String.IsNullOrEmpty(Request["listcate"]))
            {
                string shtml = "[";
                int lang = 1;                
                if (Session["SelLang"].ToString() == "vi")
                {
                    lang = 2;
                }
                else
                {
                    lang = 1;
                }
                DataTable dts = Common.Common.ConvertToDataTable(_buswrcategory._GetList(p => p.IDLang == lang));
                if (dts.Rows.Count > 0)
                {
                    for (int i = 0; i < dts.Rows.Count; i++)
                    {
                        if (i == 0)
                        {
                            shtml += "{ \"id\":" + dts.Rows[i]["ID"].ToString() + ", \"text\": \"" + dts.Rows[i]["CategoryName"].ToString() + "\" ";
                        }
                        else
                        {
                            shtml += ", { \"id\":" + dts.Rows[i]["ID"].ToString() + ", \"text\": \"" + dts.Rows[i]["CategoryName"].ToString() + "\" ";
                        }
                        //dr[0] = "9999" + dts.Rows[i]["ID"].ToString();
                        //dr[1] = dts.Rows[i]["CategoryName"].ToString();
                        //dr[2] = "99990";
                        //dtn.Rows.Add(dr);
                        DataTable dts1 = Common.Common.ConvertToDataTable(_buswrcategorylv1._GetList(p => p.IDCategory == Int32.Parse(dts.Rows[i]["ID"].ToString())));
                        if (dts1.Rows.Count > 0)
                        {
                            shtml += ", \"children\":[";
                            for (int j = 0; j < dts1.Rows.Count; j++)
                            {
                                if (j == 0)
                                {
                                    shtml += "{ \"id\":" + dts1.Rows[j]["ID"].ToString() + ", \"text\": \"" + dts1.Rows[j]["CategoryLv1Name"].ToString() + "\" }";
                                }
                                else
                                {
                                    shtml += ", { \"id\":" + dts1.Rows[j]["ID"].ToString() + ", \"text\": \"" + dts1.Rows[j]["CategoryLv1Name"].ToString() + "\" }";
                                }
                                //DataRow dr1 = dtn.NewRow();
                                //dr1[0] = dts1.Rows[j]["ID"].ToString();
                                //dr1[1] = dts1.Rows[j]["CategoryLv1Name"].ToString();
                                //dr1[2] = "9999" + dts.Rows[i]["ID"].ToString();
                                //dtn.Rows.Add(dr1);
                            }
                            shtml += "]";
                        }
                        shtml += "}";
                    }
                }
                shtml += "]";
                Response.Write(shtml);
                Response.End();
            }
            //Kiem tra thong tin dang nhap
            if (!String.IsNullOrEmpty(Request["dkuser"]))
            {
                String password = Request["dkpass"];
                String account = Request["dkuser"];
                String result = "0";
                string sMahoa = _objEncrypt.Mahoa(password);
                if (sMahoa.Equals("Không tìm thấy file data.dat") == true)
                {
                    WebMsgBox.Show("Không tìm thấy file data.dat");
                    return;
                }
                WRUSER user = new WRUSER();
                user = _busUser._GetObject(p => p.Username == account && p.Password == sMahoa && p.Active == "Y");
                if (user != null)
                {
                    Session["Username"] = account.Trim();
                    Session["userid"] = user.ID;
                    result = "1";
                }
                Response.Write(result);
                Response.End();
            }

            if (!String.IsNullOrEmpty(Request["dkuserdk"]))
            {
                String password = Request["dkpassdk"];
                String account = Request["dkuserdk"];
                String email = Request["dkemail"];
                String result = "0";
                if (kiemtra_dangky(account) == "1")
                {
                    result = "0";
                }
                else
                {
                    WRUSER user = new WRUSER();
                    string sMahoa = _objEncrypt.Mahoa(password);
                    if (sMahoa.Equals("Không tìm thấy file data.dat") == true)
                    {
                        WebMsgBox.Show("Không tìm thấy file data.dat");
                        return;
                    }
                    user.Username = account;
                    user.Password = sMahoa;
                    user.Email = email;
                    user.Active = "Y";
                    user.CreateDate = DateTime.Now.Date;
                    user.UpdateDate = DateTime.Now.Date;
                    _busUser._Add(user);
                    result = "1";
                }
                Response.Write(result);
                Response.End();
            }
        }

    }
    private string kiemtra_dangky(string account)
    {
        WRUSER user1 = new WRUSER();
        string kt = "0";
        user1 = _busUser._GetObject(p => p.Username == account && p.Active == "Y");
        if (user1 != null)
        {
            kt = "1";
        }
        else
        {
            kt = "0";
        }
        return kt;
    }
}
